﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker 
{
	
	class XSNManifestRolesParseWorker
	{
		
		public static void  parseRoles(Manifest manifest) {
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;

			string rolesFilter = NamespaceUtil.toFullName( xsfPrefix, "roles" );

			XmlElement rolesElement = (XmlElement)DOMUtil.selectANode( root, rolesFilter, manifest.NsResolver );
			if( rolesElement != null ){
				Roles roles = new Roles();
				ArrayList roleList = new ArrayList();
				ArrayList membershipList = new ArrayList();

				string defaultRole = rolesElement.GetAttribute( "default" );
				if( defaultRole.Length <= 0 ){
					throw new InfoJetException( "Cannot find roles - default attribute.", "XMRPW01" );
				}else{
					roles.DefaultRole = defaultRole;
				}
				string initiator = rolesElement.GetAttribute( "initiator" );
				if( initiator.Length > 0 ){
					roles.Initiator = initiator;
				}

				XmlNodeList roleElementList = DOMUtil.selectNodes( rolesElement, NamespaceUtil.toFullName( xsfPrefix, "role" ), 1, manifest.NsResolver );
				foreach( XmlElement roleElement in roleElementList ){
					string name = roleElement.GetAttribute( "name" );
					if( name.Length <= 0 ){
						throw new InfoJetException( "Cannot find role - name attribute.", "XMRPW02" );
					}else{
						roleList.Add( name );
					}
				}

				string GetUserNameFromData = NamespaceUtil.toFullName( xsfPrefix, "getUserNameFromData" );
				string UserName = NamespaceUtil.toFullName( xsfPrefix, "userName" );
				string Group = NamespaceUtil.toFullName( xsfPrefix, "group" );

				XmlElement membershipElement = (XmlElement)DOMUtil.selectANode( rolesElement, NamespaceUtil.toFullName( xsfPrefix, "membership" ), manifest.NsResolver );
				if( membershipElement != null ){
					foreach( XmlNode childNode in membershipElement.ChildNodes ){
						if( childNode is XmlElement ){
							XmlElement childElement = (XmlElement) childNode;
							if( childElement.Name.Equals( GetUserNameFromData ) ){
								Membership membership = new Membership();
								membership.Type = Membership.GetUserNameFromData;
								string memberOf = childElement.GetAttribute( "memberOf" );
								if( memberOf.Length <= 0 ){
									throw new InfoJetException( "Cannot find getUserNameFromData - memberOf attribute.", "XMRPW03" );
								}else{
									membership.MemberOf = memberOf;
								}
								string select = childElement.GetAttribute( "select" );
								if( select.Length <= 0 ){
									throw new InfoJetException( "Cannot find getUserNameFromData - select attribute.", "XMRPW04" );
								}else{
									membership.Select = select;
								}
								string dataObject = childElement.GetAttribute( "dataObject" );
								if( dataObject.Length > 0 ){
									membership.DataObject = dataObject;
								}
								membershipList.Add( membership );
							}else if( childElement.Name.Equals( UserName ) ){
								Membership membership = new Membership();
								membership.Type = Membership.UserName;
								string memberOf = childElement.GetAttribute( "memberOf" );
								if( memberOf.Length <= 0 ){
									throw new InfoJetException( "Cannot find userName - memberOf attribute.", "XMRPW03" );
								}else{
									membership.MemberOf = memberOf;
								}
								string name = childElement.GetAttribute( "name" );
								if( name.Length <= 0 ){
									throw new InfoJetException( "Cannot find userName - name attribute.", "XMRPW04" );
								}else{
									membership.NameList = parseNameList( name );
								}
								membershipList.Add( membership );
							}else if( childElement.Name.Equals( Group ) ){
								Membership membership = new Membership();
								membership.Type = Membership.Group;
								string memberOf = childElement.GetAttribute( "memberOf" );
								if( memberOf.Length <= 0 ){
									throw new InfoJetException( "Cannot find group - memberOf attribute.", "XMRPW03" );
								}else{
									membership.MemberOf = memberOf;
								}
								string name = childElement.GetAttribute( "name" );
								if( name.Length <= 0 ){
									throw new InfoJetException( "Cannot find group - name attribute.", "XMRPW04" );
								}else{
									membership.NameList = parseNameList( name );
								}
								membershipList.Add( membership );
							}
						}
					}
				}

				roles.RoleList = roleList;
				roles.MembershipList = membershipList;

				manifest.Roles = roles;
			}
		}

		public static ArrayList parseNameList( string names ){
			ArrayList nameList = new ArrayList();
			StringTokenizer nameTokenizer = new StringTokenizer( names, ";" );
			while( nameTokenizer.HasMoreTokens() ){
				string name = nameTokenizer.NextToken().Trim();
				if( name.Length > 0 ){
					nameList.Add( name );
				}
			}
			return nameList;
		}
	}

}